package com.google.android.exoplayer2.drm;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import androidx.annotation.NonNull;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.drm.DrmInitData.SchemeData;
import com.google.android.exoplayer2.drm.DrmSession.DrmSessionException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import o.adc;
import o.adf;
import o.adh;
import o.adj;
import o.adk;
import o.adl;
import o.adm;
import o.ado;
import o.amm;
import o.amu;
import o.amz;
import o.anr;

@TargetApi(18)
public class DefaultDrmSessionManager<T extends ado> implements adf<T>, o.adc.e<T> {
    private final List<adc<T>> ʻ;
    private final boolean ʼ;
    private final int ʽ;
    private final UUID ˊ;
    private int ˊॱ;
    private final adk<T> ˋ;
    private Looper ˋॱ;
    private final HashMap<String, String> ˎ;
    private final adl ˏ;
    private byte[] ͺ;
    volatile e ॱ;
    private final amu<adj> ॱॱ;
    private final List<adc<T>> ᐝ;

    public static final class MissingSchemeDataException extends Exception {
        private MissingSchemeDataException(UUID uuid) {
            super("Media does not support uuid: " + uuid);
        }
    }

    @SuppressLint({"HandlerLeak"})
    class e extends Handler {
        final /* synthetic */ DefaultDrmSessionManager ॱ;

        public e(DefaultDrmSessionManager defaultDrmSessionManager, Looper looper) {
            this.ॱ = defaultDrmSessionManager;
            super(looper);
        }

        public void handleMessage(Message message) {
            byte[] bArr = (byte[]) message.obj;
            for (adc o_adc : this.ॱ.ʻ) {
                if (o_adc.ˋ(bArr)) {
                    o_adc.ˏ(message.what);
                    return;
                }
            }
        }
    }

    public final void ॱ(Handler handler, adj o_adj) {
        this.ॱॱ.ˏ(handler, o_adj);
    }

    public boolean ॱ(@NonNull DrmInitData drmInitData) {
        if (this.ͺ != null) {
            return true;
        }
        if (ˏ(drmInitData, this.ˊ, true).isEmpty()) {
            if (drmInitData.ˊ != 1 || !drmInitData.ˎ(0).ˏ(C.ˋ)) {
                return false;
            }
            amz.ˏ("DefaultDrmSessionMgr", "DrmInitData only contains common PSSH SchemeData. Assuming support for: " + this.ˊ);
        }
        String str = drmInitData.ॱ;
        if (str == null || "cenc".equals(str)) {
            return true;
        }
        if (("cbc1".equals(str) || "cbcs".equals(str) || "cens".equals(str)) && anr.ˋ < 25) {
            return false;
        }
        return true;
    }

    public DrmSession<T> ˋ(Looper looper, DrmInitData drmInitData) {
        DrmSession<T> drmSession;
        AnonymousClass2 anonymousClass2 = null;
        boolean z = this.ˋॱ == null || this.ˋॱ == looper;
        amm.ˏ(z);
        if (this.ʻ.isEmpty()) {
            this.ˋॱ = looper;
            if (this.ॱ == null) {
                this.ॱ = new e(this, looper);
            }
        }
        Object ˏ;
        if (this.ͺ == null) {
            ˏ = ˏ(drmInitData, this.ˊ, false);
            if (ˏ.isEmpty()) {
                Throwable missingSchemeDataException = new MissingSchemeDataException(this.ˊ);
                this.ॱॱ.ˊ(new adh(missingSchemeDataException));
                return new adm(new DrmSessionException(missingSchemeDataException));
            }
        }
        ˏ = null;
        if (this.ʼ) {
            for (adc o_adc : this.ʻ) {
                if (anr.ˎ(o_adc.ॱ, ˏ)) {
                    break;
                }
            }
            drmSession = null;
        } else {
            if (!this.ʻ.isEmpty()) {
                Object obj = (adc) this.ʻ.get(0);
            }
            drmSession = anonymousClass2;
        }
        if (drmSession == null) {
            drmSession = new adc(this.ˊ, this.ˋ, this, ˏ, this.ˊॱ, this.ͺ, this.ˎ, this.ˏ, looper, this.ॱॱ, this.ʽ);
            this.ʻ.add(drmSession);
        }
        drmSession.ॱ();
        return drmSession;
    }

    public void ˊ(DrmSession<T> drmSession) {
        if (!(drmSession instanceof adm)) {
            adc o_adc = (adc) drmSession;
            if (o_adc.ˏ()) {
                this.ʻ.remove(o_adc);
                if (this.ᐝ.size() > 1 && this.ᐝ.get(0) == o_adc) {
                    ((adc) this.ᐝ.get(1)).ˊ();
                }
                this.ᐝ.remove(o_adc);
            }
        }
    }

    public void ˎ(adc<T> o_adc_T) {
        this.ᐝ.add(o_adc_T);
        if (this.ᐝ.size() == 1) {
            o_adc_T.ˊ();
        }
    }

    public void ˋ() {
        for (adc ˎ : this.ᐝ) {
            ˎ.ˎ();
        }
        this.ᐝ.clear();
    }

    public void ˏ(Exception exception) {
        for (adc ˋ : this.ᐝ) {
            ˋ.ˋ(exception);
        }
        this.ᐝ.clear();
    }

    private static List<SchemeData> ˏ(DrmInitData drmInitData, UUID uuid, boolean z) {
        List<SchemeData> arrayList = new ArrayList(drmInitData.ˊ);
        for (int i = 0; i < drmInitData.ˊ; i++) {
            Object obj;
            SchemeData ˎ = drmInitData.ˎ(i);
            if (ˎ.ˏ(uuid) || (C.ॱ.equals(uuid) && ˎ.ˏ(C.ˋ))) {
                obj = 1;
            } else {
                obj = null;
            }
            if (obj != null && (ˎ.ˋ != null || z)) {
                arrayList.add(ˎ);
            }
        }
        return arrayList;
    }
}
